Thread: Why does '*' not work? (switch function) [EASY]

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    Why does '*' not work? (switch function) [EASY]

    Hello everyone! =) I need some help in this case:
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    potenzieren(float basis, int exponent){ 
        int i; 
        float ergebnis = basis; 
        for(i=1;i<=exponent;i++) 
            ergebnis*=basis; 
            return ergebnis; 
    } 
    main (int argc,char *argv[]){ 
    if (argc==2){ 
        if  (strcmp(argv[1],"-help")||strcmp(argv[1],"--help")||strcmp(argv[1],"help")){ 
        /*showtooltip*/ 
        printf("You can use this programm to calculate simple formulas step  by step.\n Operators you can use are:\n'+'\t\t for addition\n'-'\t\t for  subtraction\n'*' \t\t for multiplying\n'/'\t\t for division\n'^'\t\t for  exponential formulas\n'from'\t\t to get the chance of\n\t\t getting [x]  right from [y]amount\n"); 
        } 
    } 
    
    if (argc<4){ 
        printf("Use calculator '[number] [operator] [number]' to get a  result!\n"); 
        exit(1); 
    } 
    float z; 
    if (argc==4){ 
    float x = atof(argv[1]); 
    float y = atof(argv[3]); 
    switch (argv[2][0]){ 
        case '+': 
                z=x+y; 
                break; 
        case '-': 
                z=x-y; 
                break; 
        case '/': 
                z=x/y; 
                break; 
        case '*' :        //42 = '*' ASCII 
                z=x*y; 
                break; 
        case '^': 
                z=potenzieren(x,y); 
                break; 
        default : printf("Wrong operator!\n"); 
                    break; 
    } 
    printf("%s %s %s = %f \n",argv[1], argv[2], argv[3], z); 
    
    } 
    exit(0); 
    } 
    
    Everything works right, should only be a simple programm to make easy calculations... But multiplying will not work correctly. =(
    I don't know why, I even tried entering 42 as binary code, but won't work...

    Some help? =)
    Greets

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I would be more than glad to help but the current formatting is making my eyes stretch to the point of implosion. Please copy and paste this as text within [ code ] brackets and let our site do the formatting.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're using * on the command line, so I think the shell is interpreting that as a wildcard, as shells are wont to do.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    I would suggest you put at the start of main
    Code:
    for ( i = 0 ; i < argc ; i++ ) {
      printf("Arg %d = %s\n", i, argv[i] );
    }
    What I suspect you will find is that * is one of those wildcard characters processed by your command line shell, so instead of a literal *, you have a nice list of filenames.

    One way around this is to say something like
    ./myprog "2 * 3"
    but you'll only have a single argument, and you'll have to parse out the "2" "*" and "3" yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    thanks for the answers!
    Yeah... you're right it's a wildcard (i'm using ubuntu)
    Like mentioned i could put it in a string, but is there a way to ignore those wildcards? a starting preference of the programm or something like that?

    and when i add an '-' in front of the operator will it crash too?
    ex.: ./calculator 5 -- 2
    result = 3?
    same for ./calculator 5 -* 2 (=10)
    or will it be interpreted as an wildcard again?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Ribbley View Post
    thanks for the answers!
    Yeah... you're right it's a wildcard (i'm using ubuntu)
    Like mentioned i could put it in a string, but is there a way to ignore those wildcards? a starting preference of the programm or something like that?
    You could escape it like "\*" so the shell doesn't interpret it but passes it as is to the program.
    Quote Originally Posted by Ribbley View Post
    and when i add an '-' in front of the operator will it crash too?
    ex.: ./calculator 5 -- 2
    result = 3?
    That simply depends on how well the command line parser is written.
    Quote Originally Posted by Ribbley View Post
    same for ./calculator 5 -* 2 (=10)
    or will it be interpreted as an wildcard again?
    It'll be interpreted first by the shell, that's why utilities like bc or dc perform such operations inside their own shell.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You could try this
    How do I disable pathname expansion in bash? - Ask Ubuntu - Stack Exchange

    Or perhaps this
    ./calc 2 \* 3

    Backslash is the universal "escape" character to prevent the next character from being treated as special in some way.

    Both are somewhat hacky answers
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could escape it.

    ./calculator 5 \* 3

    This always works in bash.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    LOL - everyone's all over this question like a cheap suit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    okay thanks! seems to work with \*... so i have to add it to the help text - "use '\*' to avoid wildcard usage" or smth like that... thanks !!
    I will look here more often for answers ;D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy if-else OR switch-case
    By audinue in forum Tech Board
    Replies: 5
    Last Post: 03-22-2009, 02:33 PM
  2. easy question about switch
    By zdzislavv in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2008, 09:20 AM
  3. Help!!!!!! Its easy but i cant get it to work
    By saadsaslam in forum C Programming
    Replies: 6
    Last Post: 03-13-2008, 08:40 PM
  4. Replies: 11
    Last Post: 10-04-2006, 09:19 PM
  5. Probrem With Switch (Another Easy Task):
    By Maragato in forum C Programming
    Replies: 3
    Last Post: 07-25-2004, 05:29 AM

Tags for this Thread